package com.kvn.instrument;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.tools.attach.VirtualMachine;

/**
 * @author wzy
 * @date 2017年9月21日 下午5:06:48
 */
public class Threads {

	/**
	 * 输出：
	 * <pre>
	 * JMX server connection timeout 13 / TIMED_WAITING
	 * RMI Scheduler(0) / TIMED_WAITING
	 * RMI TCP Connection(1)-192.168.31.28 / RUNNABLE
	 * RMI TCP Accept-0 / RUNNABLE
	 * Attach Listener / RUNNABLE
	 * Signal Dispatcher / RUNNABLE
	 * Finalizer / WAITING
	 * Reference Handler / WAITING
	 * main / TIMED_WAITING
	 * </pre>
	 */
	public static void main(String args[]) throws Exception {
		args = new String[] { "10764" };
		if (args.length != 1) {
			System.err.println("Please provide process id");
			System.exit(-1);
		}
		VirtualMachine vm = VirtualMachine.attach(args[0]);
		String connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
		if (connectorAddr == null) {
			String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
			vm.loadAgent(agent);
			connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
		}
		JMXServiceURL serviceURL = new JMXServiceURL(connectorAddr);
		JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
		MBeanServerConnection mbsc = connector.getMBeanServerConnection();
		ObjectName objName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
		Set<ObjectName> mbeans = mbsc.queryNames(objName, null);
		for (ObjectName name : mbeans) {
			ThreadMXBean threadBean;
			threadBean = ManagementFactory.newPlatformMXBeanProxy(mbsc, name.toString(), ThreadMXBean.class);
			long threadIds[] = threadBean.getAllThreadIds();
			for (long threadId : threadIds) {
				ThreadInfo threadInfo = threadBean.getThreadInfo(threadId);
				System.out.println(threadInfo.getThreadName() + " / " + threadInfo.getThreadState());
			}
		}
	}
	
	
}
